﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevM.Development.Projects
{
    public class ProjectStore : ITransientDependency
    {
        private readonly IRepository<Project, long> projectRepository;
        private readonly IRepository<Environment, long> environmentRepository;

        public ProjectStore(IRepository<Project, long> projectRepository, IRepository<Environment, long> environmentRepository)
        {
            this.projectRepository = projectRepository;
            this.environmentRepository = environmentRepository;
        }

        /// <summary>
        /// 项目列表
        /// </summary>
        public IQueryable<Project> Projects { get => projectRepository.GetAll(); }

        /// <summary>
        /// 根据项目获取环境
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<List<Environment>> GetEnvironmentsByProjectId(long projectId)
        {
            return await Task.Run(()=> environmentRepository.GetAll().Where(en => en.ProjectId == projectId).OrderBy(en=>en.Order).ToList());
        }
        /// <summary>
        /// 根据id获取环境
        /// </summary>
        /// <param name="environmentId"></param>
        /// <returns></returns>
        public async Task<Environment> GetEnvironmentById(long environmentId)
        {
            return await environmentRepository.GetAsync(environmentId);
        }
    }
}
